import { defineStore } from 'pinia'
import { reqAddOrUpdataSkuInfoToShopCart, reqChangeAllCartState, reqChangeShopCartInfoState, reqDelOneShopCartInfo, reqGetSkuInfoToShopCart, reqUserAddressList } from '../api/cart';
import { nanoid } from 'nanoid';
export default defineStore('useCartstore', {
  state: () => {
    return {
      cartInfoList: [],
      allisChecked: null,
      skuNum: 0,
      timer: '',
      addressArr: [],
      buyAddress: {},
      chosenAddressId: 0,
      chosenAddress: null

    }
  },
  actions: {
    // 获取地址
    getAddress() {
      if (!localStorage.getItem('addressArr')) {
        this.addressArr = [];
      } else {
        this.addressArr = JSON.parse(localStorage.getItem('addressArr'));
      }
    },
    // 保存地址
    addAddress(contactInfo, flay, index) {
      const { province, country, city, county, addressDetail, isDefault } = contactInfo;
      contactInfo.address = province + country + county + city + addressDetail;
      contactInfo.id = nanoid()
      if (isDefault) {
        this.addressArr.forEach(item => {
          item.isDefault = false
        })
      }
      if (!flay) {
        this.addressArr.push(contactInfo);
      } else {
        this.addressArr[index] = contactInfo
      }
      localStorage.setItem('addressArr', JSON.stringify(this.addressArr));
    },
    // 删除地址
    removeAddress(id) {
      this.addressArr = this.addressArr.filter(item => item.id !== id)
      localStorage.setItem('addressArr', JSON.stringify(this.addressArr));
    },

    // 购物车数据
    async getCartlist() {
      const result = await reqGetSkuInfoToShopCart()
      if (result.length == 0) return
      this.cartInfoList = result[0].cartInfoList
      this.isAllcheck()
    },

    // 判断是否全选
    isAllcheck() {
      this.allisChecked = this.cartInfoList.every(item => item.isChecked == 1)
    },

    // 单选
    async danxuan(item) {
      let id = item.skuId;
      let isChecked = 1 - item.isChecked;
      await reqChangeShopCartInfoState(id, 1 - isChecked);
      this.cartInfoList.forEach(item => {
        if (item.skuId == id) {
          item.isChecked == 1 - item.isChecked
        }
      })
      this.isAllcheck()
    },
    // 全选
    async allChecked() {
      this.cartInfoList.forEach(item => item.isChecked = this.allisChecked)
      await reqChangeAllCartState(+this.allisChecked, this.cartInfoIdList);
    },
    // 加加减减
    async changeNum(skuid, type, num) {
      this.cartInfoList.forEach(item => {
        if (item.skuId == skuid) {
          // 加
          if (type) {
            item.skuNum++
            this.skuNum++
          } else {
            // 减
            item.skuNum--
            this.skuNum--
          }
        }
      })
      clearTimeout(this.timer)
      this.timer = setTimeout(async () => {
        await reqAddOrUpdataSkuInfoToShopCart(skuid, this.skuNum)
        this.skuNum = 0
      }, 500)
    },
    async deleteGood(skuid, index) {
      this.cartInfoList.splice(index, 1)
      await reqDelOneShopCartInfo(skuid)
    }
  },
  getters: {
    // skuId数组
    cartInfoIdList() {
      return this.cartInfoList.map(item => item.skuId)
    },
    // 计算总价
    allprice() {
      let allPrice = 0
      this.cartInfoList.forEach(item => {
        if (item.isChecked == 1) {
          allPrice += item.skuPrice * item.skuNum
        }
      })
      return allPrice
    },
    // 默认的地址
    baseAddress() {
      return this.addressArr.filter(item => {
        if (item.isDefault) {
          return item
        }
      })[0]
    },
  }
})